package cn.player.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.makeramen.roundedimageview.RoundedImageView;
import com.startchat.filter.filter.FilterManager;
import com.startchat.filter.image.ImageFilterHelper;

import cn.player.R;


/**
 * 滤镜界面适配器
 */
public class VideoFilterAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context mContext;
    private FilterManager.FilterType[] filterTypes;
    private OnFilterConfigClickListener mOnFilterConfigClickListener;
    private int mCurrentPosition;
    private ImageFilterHelper mImageFilterHelper;

    public VideoFilterAdapter(Context context) {
        this.mContext = context;
        this.filterTypes = mFilterTypes;
        mImageFilterHelper = ImageFilterHelper.getInstance();
    }

    public void setOnFilterConfigClickListener(OnFilterConfigClickListener l) {
        this.mOnFilterConfigClickListener = l;
    }

    public void setCurrentPosition(int position) {
        mCurrentPosition = position;
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new FilterHolder(View.inflate(mContext, R.layout.video_filter_item, null));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        final FilterHolder filterHolder = (FilterHolder) holder;
        filterHolder.tvFilter.setText(FilterTypeHelper.FilterType2Name(filterTypes[position]));

        filterHolder.igvFilterBg.setBorderWidth(position == mCurrentPosition ? 3f : 0f);
//        filterHolder.igvFilterBg.setImageResource(FilterTypeHelper.FilterType2Thumb(filterTypes[position]));
        mImageFilterHelper.displayFilterImage(mContext, filterHolder.igvFilterBg, R.drawable.bg_filter, filterTypes[position]);
        filterHolder.rlFilterRoot.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mOnFilterConfigClickListener != null) {
                    mOnFilterConfigClickListener.onFilterConfigClick(position, filterTypes[position]);
                }
            }
        });
    }


    @Override
    public int getItemCount() {
        return filterTypes.length;
    }

    public class FilterHolder extends RecyclerView.ViewHolder {

        public RelativeLayout rlFilterRoot;
        public RoundedImageView igvFilterBg;
        public TextView tvFilter;

        public FilterHolder(View view) {
            super(view);
            rlFilterRoot = (RelativeLayout) view.findViewById(R.id.rl_filter_root);
            igvFilterBg = (RoundedImageView) view.findViewById(R.id.igv_filter_bg);
            tvFilter = (TextView) view.findViewById(R.id.tv_filter_item_name);
        }
    }

    public interface OnFilterConfigClickListener {
        void onFilterConfigClick(int position, FilterManager.FilterType filterType);
    }

    //Normal, Blend, SoftLight, ToneCurve, Beauty,SkinWhite,Antique,Nostalgia
    private final FilterManager.FilterType[] mFilterTypes = new FilterManager.FilterType[]{
            FilterManager.FilterType.Normal,
            FilterManager.FilterType.Beauty,
            FilterManager.FilterType.Blend,
            FilterManager.FilterType.SoftLight,

            FilterManager.FilterType.SkinWhite,
            FilterManager.FilterType.ToneCurve,
            FilterManager.FilterType.Antique,
            FilterManager.FilterType.Nostalgia,
            FilterManager.FilterType.COOL,
            FilterManager.FilterType.Healthy,
            FilterManager.FilterType.CALM,
            FilterManager.FilterType.WARM,
            FilterManager.FilterType.TENDER
    };

}
